# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: speps <speps at aur dot archlinux dot org>
# Contributor: osc <farid at archlinux-br.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - added recommended cmake defines (last two lines)

_name=SuperCollider
pkgname=supercollider
pkgver=3.11.2
pkgrel=3
pkgdesc="Environment and programming language for real time audio synthesis and algorithmic composition"
arch=('x86_64')
url="https://supercollider.github.io"
license=('GPL3')
groups=('pro-audio')
depends=('gcc-libs' 'glibc'  'libx11' 'qt5-base' 'qt5-svg' 'qt5-webengine'
'qt5-websockets')
makedepends=('abletonlink' 'alsa-lib' 'avahi' 'boost' 'cmake' 'emacs' 'fftw'
'libsndfile' 'qt5-tools' 'readline' 'systemd-libs' 'yaml-cpp')
checkdepends=('xorg-server-xvfb')
optdepends=('emacs: emacs interface'
            'gedit: gedit interface'
            'sc3-plugins: additional extension plugins for scsynth')
provides=('libscsynth.so' "sclang=${pkgver}" "scsynth=${pkgver}")
source=("https://github.com/${pkgname}/${pkgname}/releases/download/Version-${pkgver}/${_name}-${pkgver}-Source.tar.bz2"{,.asc}
        "${pkgname}-3.11.1-devendor_abletonlink.patch::https://github.com/supercollider/supercollider/pull/4836/commits/95bd9721226f3cb1bba903bea8154cee5859cfb8.patch"
        "${pkgname}-3.11.2-fix_4992.patch::https://github.com/dvzrv/supercollider/commit/9563fb5e73425a2ee0370162178964cceb559d5d.patch"
        "${pkgname}-3.11.2-boost-1.74.patch::https://github.com/supercollider/supercollider/commit/cc873049ff9249a33567e4cbcdeffeb6806a30d6.patch"
)
install="${pkgname}.install"
sha512sums=('636062c6acf8be43260632287b587f167352079c4e86ede34d1c6056180bfdb3ae50d6a3ff4f349327a3ab6af0eb01ac503d6b22667b4799dfcddd9c6489a23c'
            'SKIP'
            '6a31c8c2edd102b806cfe110c2f27516e9fe05a7b074d50e14d5aad69cb80a2badfcb3a20f942d6dee90dfaf4bf5ae7eb8b8ef8c517ea8e0110bee7f0c2c4f40'
            '9cf9f96c91f267a9f1a4ab423731d87c808acee84c06a81f58821caffab433d099d198df85f1e3829e772d59028907eb65fec155c6e31fee010e2573084b5c7c'
            'e50660ec5ecd19464ecfe22a0a4cab2df58c9eefb3d19c71763b95d2a0e8fecd7feb4fa3722965e10c3e7f36151fc255c9a8af22caee9d00f1d29af3860569ba')
b2sums=('3713561e5ebee6dd4c08baa8aaaf4a422ab983480c76b2176373ce3cd080128f9392b50482e02dc82dce235e928d058c42722e0863dad9f6d2dd086894cd85da'
        'SKIP'
        'c589ea2451b4c232fa34de1b442e6822fb5867475b0e0bb7b94563cbfac54f09f47a7be8cf1e9f4a2896ffa678db40d718ec9ee301ad037eaebeb71772e773e4'
        '1037589e0210d4ca86986a44c657539d2ffb6ca1684777b5bd38dfe3fdadfcd3d14df4d16a4af29643ca15647e96915e99866bb04626e8947ab38ae4d303225f'
        'c3eafd046222c43e06f83eb222da96bf5a4410fbe46894e1dda36c096a7656a56ef9a193b54767fd4eb835cefe56024e75ade31ed1d7721812bd0a471876a02a')
validpgpkeys=('0B7D1EA4E32B5C3C4275D3C70796CC538BEFF177') # Brian Heim <self@brianlheim.com>


prepare() {
  mv -v "${_name}-${pkgver}-Source" "${pkgname}-${pkgver}"
  cd "${pkgname}-${pkgver}"
  # devendoring Ableton Link:
  # https://github.com/supercollider/supercollider/issues/4818
  # https://github.com/supercollider/supercollider/pull/4836
  echo "Applying patch: ../${pkgname}-3.11.1-devendor_abletonlink.patch"
  patch -Np1 -i "../${pkgname}-3.11.1-devendor_abletonlink.patch"
  # fix linker issues with libscsynth.so (backported patch)
  # https://github.com/supercollider/supercollider/issues/4992
  echo "Applying patch: ../${pkgname}-3.11.2-fix_4992.patch"
  patch -Np1 -i "../${pkgname}-3.11.2-fix_4992.patch"

  echo "Applying patch: ../${pkgname}-3.11.2-boost-1.74.patch"
  patch -Np1 -i "../${pkgname}-3.11.2-boost-1.74.patch"
}

build() {
  cd "${pkgname}-${pkgver}"
  export CFLAGS+=" -DNDEBUG"
  export CXXFLAGS+=" -DNDEBUG"
  cmake -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_BUILD_TYPE='None' \
        -DBoost_NO_BOOST_CMAKE=ON \
        -DLIBSCSYNTH=ON \
        -DFORTIFY=ON \
        -DSYSTEM_ABLETON_LINK=ON \
        -DSYSTEM_BOOST=ON \
        -DSYSTEM_YAMLCPP=ON \
        -DSC_VIM=OFF \
        -Wno-dev \
        -B build \
        -DSSE=OFF -DSSE2=OFF -DSUPERNOVA=OFF -DNOVA_SIMD=ON -DNATIVE=OFF \
        -DCMAKE_C_FLAGS="${CFLAGS}" \
        -S .
  make VERBOSE=1 -C build
}

check() {
  cd "${pkgname}-${pkgver}"
  xvfb-run make test ARGS="-VV -d -j1" -C build
}

package() {
  depends+=('libasound.so' 'libavahi-common.so' 'libavahi-client.so'
  'libboost_filesystem.so' 'libboost_program_options.so' 'libboost_regex.so'
  'libboost_thread.so' 'libfftw3f.so' 'libjack.so' 'libsndfile.so'
  'libreadline.so' 'libudev.so' 'libyaml-cpp.so')
  cd "${pkgname}-${pkgver}"
  make DESTDIR="${pkgdir}" install -C build
  install -vDm 644 {AUTHORS,{CHANGELOG,README,README_LINUX}.md} \
    -t "${pkgdir}/usr/share/doc/${pkgname}/"
}
